package com.example.demo.mq;


import com.example.demo.config.artemis.ActiveMqManager;
import org.apache.activemq.artemis.api.core.Message;
import org.apache.activemq.artemis.api.core.QueueConfiguration;
import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.api.core.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class First {
    private static final Logger logger = LoggerFactory.getLogger(First.class);

    public static void main(String[] args) {
        ServerLocator serverLocator = null;
        ClientSessionFactory factory = null;
        String queueName = "jinxin_queue_02";
        String addressName = "jinxin_address_01";
        try {
            serverLocator = ActiveMQClient.createServerLocator("tcp://127.0.0.1:61616");
            serverLocator.setPreAcknowledge(Boolean.TRUE);
            factory = serverLocator.createSessionFactory();
            ClientSession session = factory.createSession();
            ClientSession.QueueQuery queueQuery =
                    session.queueQuery(new SimpleString(queueName));

            if(!queueQuery.isExists()) {
                ClientSession.AddressQuery addressQuery = session.addressQuery(new SimpleString(addressName));
                if(!addressQuery.isExists()) {
                    RoutingType[] types = {
                            RoutingType.ANYCAST,
                            RoutingType.MULTICAST
                    };
                    EnumSet<RoutingType> set = EnumSet.copyOf(Arrays.asList(types));

                    session.createAddress(new SimpleString(addressName),
                            set,
                            false);
                }
                QueueConfiguration queueConfiguration =
                        new QueueConfiguration(queueName);
                queueConfiguration.setAddress(addressName);
                queueConfiguration.setRoutingType(RoutingType.MULTICAST);
                queueConfiguration.setDurable(true);
                //queueConfiguration.setLastValue(true);
                //queueConfiguration.setExclusive(true);
                //queueConfiguration.setAutoDelete(true);
                //queueConfiguration.setDurable(false);
                session.createQueue(queueConfiguration);
                logger.debug("Queue name is created with address.");
            }

//            session.start();
//            ClientProducer producer = session.createProducer(addressName);
//            ClientMessage message = session.createMessage(false);
//            message.setExpiration(System.currentTimeMillis() + 3000 * 1000);
//            message.getBodyBuffer().writeString("Hello World. " + new Date());
//            producer.send(message);
//            session.close();


        } catch (Exception e) {
            logger.error("Failed to create client session factory, reason: {}", e.getMessage());
        } finally {
            if(factory != null) {
                factory.close();
            }
        }
    }
}
